package zhao.exmaple;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SerializationSchema;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.connector.file.src.reader.TextLineFormat;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer.Semantic;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer011;
import org.apache.flink.streaming.util.serialization.KeyedSerializationSchema;

import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class KafkaSinkDemo {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment();
        // 没两秒钟检测并关闭流
        env.enableCheckpointing(1000, CheckpointingMode.EXACTLY_ONCE);


        TextLineFormat textLineFormat = new TextLineFormat();
        Path path = new Path("E:\\note\\flink-project\\temp\\word.txt");
        FileSource<String> source = FileSource.forRecordStreamFormat(textLineFormat, path).build();

        DataStreamSource<String> dataSource = env.fromSource(source, WatermarkStrategy.noWatermarks(), "filesourcdemo");


        SerializationSchema<String> serializationSchema = new SimpleStringSchema();
        // todo 方法一
        // FlinkKafkaProducer<String> myProducer = new FlinkKafkaProducer<>("node1:9092,node2:9092,node3:9092", "test", serializationSchema);


        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "node1:9092,node2:9092,node3:9092");
        properties.setProperty("group.id", "flink-group");
        // todo 方法二
        FlinkKafkaProducer<String> myProducer = new FlinkKafkaProducer<String>("test", serializationSchema, properties);
        dataSource.addSink(myProducer);

        env.execute();
    }
}
